package fanxinghao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;


public class Surface extends JFrame implements ActionListener {

    //参数、变量声明
    final int columnWidth=12;
    String[] codeText = {"2010001","2010002","2010003","2010004","2010005"};
    JComboBox<String> code=new JComboBox<String>(codeText);
    JTextField name = new JTextField(columnWidth);
    JRadioButton yellow = new JRadioButton("黄");
    JRadioButton red = new JRadioButton("红");
    JRadioButton blue = new JRadioButton("蓝");
    JTextField price = new JTextField(columnWidth);
    JTextField count = new JTextField(columnWidth);
    JButton buy = new JButton("购买");
    JButton inquire = new JButton("查询");
    JButton saveFile = new JButton("保存到文件");
    JButton saveDB = new JButton("保存到数据库");
    DefaultListModel<String> ListModel = new DefaultListModel<String>();
    JList<String> list = new JList<>(ListModel);
    JScrollPane scrollPane = new JScrollPane(list);
    JPanel upPanel = new JPanel(new GridLayout(2,1));
    JPanel downPanel = new JPanel(new GridLayout(1,2));
    JPanel temp;
    ButtonGroup group = new ButtonGroup();
    MyDB db = new MyDB("SalesDB");
    public int rowCount = 0;

    public Surface() throws SQLException, ClassNotFoundException {
        super("201800800504樊昕昊");
        this.setBounds(0,0,550,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());

        //region $上部
        this.add(upPanel, BorderLayout.NORTH);
        temp = new JPanel(new GridLayout(1,7));
        temp.add(new JLabel("产品代号"));
        temp.add(code);
        temp.add(new JLabel("产品名称"));
        temp.add(name);
        group.add(yellow);group.add(red);group.add(blue);
        temp.add(yellow);temp.add(red);temp.add(blue);yellow.setSelected(true);
        upPanel.add(temp);

        temp = new JPanel(new GridLayout(1,6));
        temp.add(new JLabel("单价"));
        temp.add(price);
        temp.add(new JLabel("数量"));
        temp.add(count);
        temp.add(buy);temp.add(inquire);
        upPanel.add(temp);
        //endregion

        //region $中部
        this.add(scrollPane,BorderLayout.CENTER);
        //endregion

        //region $下部
        downPanel.add(saveFile);downPanel.add(saveDB);
        this.add(downPanel,BorderLayout.SOUTH);
        //endregion

        //region $动作监听器
        this.buy.addActionListener(this);
        this.inquire.addActionListener(this);
        this.saveFile.addActionListener(this);
        this.saveDB.addActionListener(this);
        //endregion

        this.setVisible(true);
    }

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        new Surface();
    }
    @Override
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==buy){
            if("".equals(name.getText())||name.getText()==null
                    ||"".equals(price.getText())||price.getText()==null
                    ||"".equals(count.getText())||count.getText()==null){
                JOptionPane.showMessageDialog(this,"请输入完整信息");
                return;
            }
            try{
                String add = code.getSelectedItem() + " " + name.getText() + " ";
                if (yellow.isSelected()) {
                    add += "黄 ";
                } else if (red.isSelected()) {
                    add += "红 ";
                } else if (blue.isSelected()) {
                    add += "蓝 ";
                }
                add += price.getText() + " " + count.getText() + " " +
                        Double.parseDouble(price.getText()) * Integer.parseInt(count.getText());
                ListModel.addElement(add);
                rowCount++;
            }catch(NumberFormatException err){
                JOptionPane.showMessageDialog(this,"输入错误");
            }
        }
        else if(e.getSource()==inquire){
            String sql = "SELECT * FROM SalesRecord where sno = "+"'"+code.getSelectedItem()+"'";
            try {
                db.query(sql,list);
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        else if(e.getSource()==saveFile) {
            String save;
            try {
                for (int i = 0; (save = ListModel.getElementAt(i)) != null && !"".equals(save); i++) {
                    save += "\n";
                    FileIO.writeLine("goumai.txt", save);
                }
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "保存失败");
                //ex.printStackTrace();
            } catch (ArrayIndexOutOfBoundsException ignored) {
            }
        }
        else if(e.getSource()==saveDB){
            String[] save;
            try {
                for (int i = 0; (save = ListModel.getElementAt(i).split(" ")) != null
                        && !"".equals(ListModel.getElementAt(i)); i++) {
                    if("黄".equals(save[2])){save[2] ="Y";}
                    else if("红".equals(save[2])){save[2] ="R";}
                    else if("蓝".equals(save[2])){save[2] ="B";}
                    String sql = String.format("insert SalesRecord " +
                                    "values('%s','%s','%s',%f,%d,%f)"
                            ,save[0],save[1],save[2],Double.parseDouble(save[3])
                    ,Integer.parseInt(save[4]),Double.parseDouble(save[5]));
                    db.insert(sql);
                }
            } catch (ArrayIndexOutOfBoundsException ignored) {
            }
        }
    }
}
